#!/bin/bash
parameter="$1"

#四个版本的名称
str1="master"
str1_start="master"
str2="OpenHarmony-3.2-beta2"
str2_start="OpenHarmony-3.2-beta2"
str3="OpenHarmony-3.2-beta2-Merge"
str3_start="OpenHarmony-3.2-beta2"
str4="OpenHarmony-3.2-Release"
str4_start="OpenHarmony-3.2-Release"

#下载路径
function download_path()
{
	if [ ! -e $parameter ];then
		mkdir $parameter
		cd $parameter
	elif [ ! -e "$parameter"_tmp ];then
		mkdir "$parameter"_tmp
		cd "$parameter"_tmp
	else
	       	rm -rf "$parameter"_tmp
		mkdir "$parameter"_tmp
		cd "$parameter"_tmp
	fi
}

#下载信息
url="https://gitee.com/openharmony/manifest"
sync_cmd="repo sync -c"
lfs_cmd="repo forall -c 'git lfs pull'"
function download_code()
{
	if [ "$parameter" = "$str1" ];then
		init_cmd="repo init -u $url -b $parameter --no-repo-verify"
		set_br_cmd="repo start $str1_start"
	elif [ "$parameter" = "$str2" ];then
		init_cmd="repo init -u $url -b $parameter --no-repo-verify"
		set_br_cmd="repo start $str2_start"
	elif [ "$parameter" = "$str3" ];then
		xml_name="thead_develop.xml"
		init_cmd="repo init -u $url -b $parameter -m $xml_name --no-repo-verify"
		set_br_cmd="repo start $str3_start"
	elif [ "$parameter" = "$str4" ];then
		xml_name="default.xml"
		init_cmd="repo init -u $url -b $parameter -m $xml_name --no-repo-verify"
		set_br_cmd="repo start $str4_start"
	fi 
}

#打印信息
function download_information()
{
	echo -e "\e[1;33m you have been download OpenHarmony \e[0m\e[31m$parameter\e[0m \e[1;33mcode \e[0m"
	echo -e "\e[1;34m url       : \e[0m\e[35m$url \e[0m"
	echo -e "\e[1;34m branch    : \e[0m\e[35m$parameter \e[0m"
	echo -e "\e[1;34m xml_name  : \e[0m\e[35m$xml_name \e[0m"
	echo -e "\e[1;34m code_path : \e[0m\e[35m$code_path \e[0m"
	echo -e "\e[1;34m init   cmd: \e[0m\e[35m$init_cmd \e[0m"
	echo -e "\e[1;34m sync   cmd: \e[0m\e[35m$sync_cmd \e[0m"
	echo -e "\e[1;34m lfs    cmd: \e[0m\e[35m$lfs_cmd \e[0m"
	echo -e "\e[1;34m set_br cmd: \e[0m\e[35m$set_br_cmd \e[0m"

}

#转换参数
num=""
function num_parameter()
{
	if [ $num == "1.1" ];then
		parameter="$str1"
	elif [ $num == "1.2" ];then
		parameter="$str2"
	elif [ $num == "1.3" ];then
		parameter="$str3"
	elif [ $num == "1.4" ];then
		parameter="$str4"
	fi
	
}

#实现下载功能
function download_realize()
{
	download_path
        code_path=$(pwd)
        download_code
        download_information
}

#打印菜单
function print_menu()
{
	echo -e "\e[5;37m ******************************************* \e[0m"
	echo -e "\e[5;37m *\e[0m \e[1;5;33m Welcome to download OpenHarmony Code \e[0m \e[5;37m *\e[0m"
	echo -e "\e[5;37m *\e[0m \e[1;36m Please Choice OpenHarmony SDK:       \e[0m \e[5;37m *\e[0m "
	echo -e "\e[5;37m *\e[0m \e[1;34m OpenHarmony Version                  \e[0m \e[5;37m *\e[0m "
	echo -e "\e[5;37m *\e[0m \e[1;35m OpenHarmony master          \e[0m\e[31mprese 1.1 \e[0m \e[5;37m*\e[0m "
	echo -e "\e[5;37m *\e[0m \e[1;35m OpenHarmony-3.2-beta2       \e[0m\e[31mprese 1.2 \e[0m \e[5;37m*\e[0m "
	echo -e "\e[5;37m *\e[0m \e[1;35m OpenHarmony-3.2-beta2-merge \e[0m\e[31mprese 1.3 \e[0m \e[5;37m*\e[0m "
	echo -e "\e[5;37m *\e[0m \e[1;35m OpenHarmony-3.2-release     \e[0m\e[31mprese 1.4 \e[0m \e[5;37m*\e[0m "
	echo -e "\e[5;37m ******************************************* \e[0m" 
	read -p '' num
	num_parameter
	download_realize
}

#实现下载功能
if [ "$parameter" = "$str1" -o "$parameter" = "$str2" -o "$parameter" = "$str3" -o "$parameter" = "$str4"  ];then
	download_realize
elif [ -z "$parameter" ];then
	print_menu
else echo -e "\e[5;31m product is not in product list, please recheck!!! \e[0m"
	print_menu
fi
